﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCS.UI.Utils;

namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 工程查询
    /// </summary>
    /// Title:ZMCS20,ServiceZMCS20
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张小飞
    /// Version: 1.0
    /// History:
    ///       2011-06-01  张小飞   [创建]
    ///
    public partial class ZMCS50 : EFViewCmes
    {
        private EngineeringBlock _block = new EngineeringBlock();

        public ZMCS50()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(_block.GetCategory());//下拉框
            base.Initialize(initData);
        }

        /// <summary>
        /// 下拉框改变下拉项的值所触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void category_ValueChanged(object sender, Platform.Core.UI.Controls.ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            this.tree.eBlockName = this.category.GetDataValue().ToString();
            this.tree.Refresh();

        }

        /// <summary>
        /// 给下拉框赋初始值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void category_Loaded(object sender, RoutedEventArgs e)
        {
            this.category.Value = "$faceRoadway";
        }

        /// <summary>
        /// 点击树的某个节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            EFTreeItem _item = this.tree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = getTreeNodeInfo(this.tree);
            if (args != null)
            {
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCS51", args);
            }
        }

        /// <summary>
        /// 获取tree选中的结点对应的label，tableName
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> getTreeNodeInfo(EFTree tree)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string value = "";
            string tableName = "";
            if (this.tree.SelectedItems.Count > 0)
            {
                EFTreeItem _item = this.tree.SelectedItems[0] as EFTreeItem;
                value = _item.ItemObject.GetType().GetProperty("value").GetValue(_item.ItemObject, null) as string;
                tableName = _item.ItemObject.GetType().GetProperty("tableName").GetValue(_item.ItemObject, null) as string;
                string isLeaf = (_item.IsLeaf == true) ? "1" : "0";
                args.Add("isLeaf", isLeaf);
                args.Add("value", value);
                args.Add("tableName", tableName);
                args.Add("comboBoxItem", this.category.GetDataValue().ToString());
                return args;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0 && treeItem.Index == 0)
            {
                this.tree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }
    }
}
